package com.atguigu.eduservice.service.impl;

import com.atguigu.common.exception.GuliException;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapvdeovo.ChapterVo;
import com.atguigu.eduservice.entity.chapvdeovo.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-13
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    //根据课程id查询课程里面的章节和小节
    @Override
    public List<ChapterVo> getCourseChapterVideo(String courseId) {
        //1 根据课程id查询课程所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2 根据课程id查询课程所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建最终封装数据list集合
        List<ChapterVo> finalChapterVideoList = new ArrayList<>();

        //3 遍历查询所有章节，进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            //得到每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter对象值复制到ChapterVo对象里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把vo对象放到list集合
            finalChapterVideoList.add(chapterVo);

            //创建小节封装list集合
            List<VideoVo> videoVoList = new ArrayList<>();
            //4 遍历查询所有小节，比较封装
            for (int m = 0; m < eduVideoList.size(); m++) {
                //得到每个小节
                EduVideo eduVideo = eduVideoList.get(m);
                //判断：比较小节chapter_id和章节id是否一样
                if(eduVideo.getChapterId().equals(eduChapter.getId())) {
                    //对象值复制
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到list集合
                    videoVoList.add(videoVo);
                }
            }
            //把章节里面小节放到章节对象里面
            chapterVo.setChildren(videoVoList);
        }
        return finalChapterVideoList;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询章节里面是否有小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        //count方法查询记录数
        int count = videoService.count(wrapper);
        //判断
        if(count > 0) { //章节里面有小节
            //不进行删除
            throw new GuliException(20001,"不能删除");
        } else { //没有小节
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    //2 根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
